package sistkaraoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import sistkaraoke.excepcion.DAOExcepcion;
import sistkaraoke.modelo.*;
import sistkaraoke.util.ConexionBD;
import sistkaraoke.dao.BaseDAO;

public class SalaDAO extends BaseDAO implements ISalaDAO {
	
	public int insertar(ISala objSala) throws DAOExcepcion {
		String query = "Exec KARAOKE.USP_ADD_SALA ?,?,?,?;";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		int i = 0;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, objSala.getNombre());
			stmt.setInt(2, objSala.getCapacidad());
			stmt.setString(3, objSala.getDescripcion());
			stmt.setInt(4, objSala.getLocal().getId());			
			
			i = stmt.executeUpdate();
			
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		
		return i;
	}
	
	public int actualizar(ISala objSala) throws DAOExcepcion {
		String query = "Exec KARAOKE.USP_UPDATE_SALA ?,?,?,?,?;";
		Connection con = null;
		PreparedStatement stmt = null;
		int i = 0;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, objSala.getId());
			stmt.setString(2, objSala.getNombre());
			stmt.setInt(3, objSala.getCapacidad());
			stmt.setString(4, objSala.getDescripcion());
			stmt.setInt(5, objSala.getLocal().getId());
			
			i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return i;
	}
	
	public int eliminar(int idSala) throws DAOExcepcion {
		String query = "EXEC KARAOKE.USP_DELETE_SALA ?;";
		Connection con = null;
		PreparedStatement stmt = null;
		int i = 0;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idSala);
			
			i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return i;
	}
	
	public Collection<ISala> buscar(ISala objSala) throws DAOExcepcion {
		String query = "EXEC KARAOKE.USP_GET_SALA ?,?,?,?;";
		Collection<ISala> lista = new ArrayList<ISala>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, objSala.getNombre());			
			stmt.setInt(2, objSala.getLocal().getId());
			stmt.setInt(3, objSala.getId());
			stmt.setInt(4, objSala.getLocal().getAdministrador().getId());
			rs = stmt.executeQuery();
			
			ISala vlSala = null;
			ILocal vlLocal = null;
			
			while (rs.next()) {
				vlSala = new Sala();
				vlSala.setId(rs.getInt("idsala"));
				vlSala.setNombre(rs.getString("nombreSala"));
				vlSala.setCapacidad(rs.getInt("capacidad"));
				vlSala.setDescripcion(rs.getString("descripcion"));
				vlLocal = new Local();
				vlLocal.setId(rs.getInt("idlocal"));
				vlLocal.setNombre(rs.getString("nombreLocal"));
				vlSala.setLocal(vlLocal);
				lista.add(vlSala);
			}
		} catch (SQLException e) {			
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}		
		return lista;
	}
	
	public Collection<ILocal> listarLocal(ILocal objLocal) throws DAOExcepcion {
		String query = "EXEC KARAOKE.USP_GET_LOCAL_FOR_SALA ?;";
		Collection<ILocal> lista = new ArrayList<ILocal>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, objLocal.getAdministrador().getId());
			rs = stmt.executeQuery();
						
			ILocal vlLocal = null;
			
			while (rs.next()) {				
				vlLocal = new Local();
				vlLocal.setId(rs.getInt("idlocal"));
				vlLocal.setNombre(rs.getString("nombre"));				
				lista.add(vlLocal);
			}
		} catch (SQLException e) {			
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}		
		return lista;
	}
	
	public Collection<ISala> ListarSalaDisponible(ISala objSala) throws DAOExcepcion {
		String query = "EXEC KARAOKE.USP_GET_SALADISPONOBLE ?;";
		Collection<ISala> lista = new ArrayList<ISala>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, objSala.getLocal().getId());			
			rs = stmt.executeQuery();
			
			ISala vlSala = null;
			ILocal vlLocal = null;
			
			while (rs.next()) {
				vlSala = new Sala();
				vlSala.setId(rs.getInt("idsala"));
				vlSala.setNombre(rs.getString("nombreSala"));
				vlSala.setCapacidad(rs.getInt("capacidad"));
				vlSala.setDescripcion(rs.getString("descripcion"));
				vlLocal = new Local();
				//vlLocal.setId(rs.getInt("idlocal"));
				vlLocal.setNombre(rs.getString("nombreLocal"));
				vlSala.setLocal(vlLocal);
				lista.add(vlSala);
			}
		} catch (SQLException e) {			
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}		
		return lista;
	}
}
